package com.binhthuan.catechist.client.content.training;

import com.binhthuan.catechist.client.AbstractContent;
import com.binhthuan.catechist.client.ContentFactory;
import com.binhthuan.catechist.client.ECApplication;
import com.binhthuan.catechist.client.ECIcons;
import com.binhthuan.catechist.client.data.AttendanceDS;
import com.binhthuan.catechist.client.data.AttendanceStatusDS;
import com.binhthuan.catechist.client.data.ScheduleDS;
import com.binhthuan.catechist.client.grid.AttendanceGrid;
import com.binhthuan.catechist.shared.Utilities;
import com.binhthuan.catechist.shared.constant.Parameter;
import com.smartgwt.client.types.VisibilityMode;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.ImgButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.events.ChangeEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangeHandler;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.layout.SectionStack;
import com.smartgwt.client.widgets.layout.SectionStackSection;
import com.smartgwt.client.widgets.layout.VLayout;

public class AttendancePage extends AbstractContent {

    public static class Factory implements ContentFactory {

        private static Factory instance;
        private String id;

        private Factory() {
            if (id == null) {
                id = Utilities.generateID(AttendancePage.class.getName());
            }
        }

        /**
         * @return the instance
         */
        public static Factory getInstance() {
            if (instance == null) {
                instance = new Factory();
            }

            return instance;
        }

        @Override
        public boolean checkCertificated(long securityCode) {
            return true;
        }

        @Override
        public AbstractContent create() {
            AttendancePage panel = new AttendancePage();
            return panel;
        }

        @Override
        public String getID() {
            return id;
        }

        @Override
        public String getIcon() {
            return ECIcons.getIcon("attendance.png");
        }

        @Override
        public String getTitle() {
            return ECApplication.getConstants().attendance();
        }
    }

    private VLayout reportViewer;
    private AttendanceGrid attendanceList;

    @Override
    public Canvas getContentPanel() {
        VLayout mainLayout = new VLayout(5);
        mainLayout.setWidth100();
        mainLayout.setHeight100();

        SectionStack stack = new SectionStack();
        stack.setWidth100();
        stack.setHeight100();
        stack.setAnimateSections(true);
        stack.setVisibilityMode(VisibilityMode.MULTIPLE);
        stack.setShowEdges(true);

        reportViewer = new VLayout();
        reportViewer.setHeight100();
        reportViewer.setWidth100();

        SectionStackSection reportSection = new SectionStackSection(ECApplication.getConstants().attendanceReport());
        reportSection.setExpanded(true);
        reportSection.setCanCollapse(false);

        ImgButton refeshButton = new ImgButton();
        refeshButton.setSrc(ECIcons.getIcon("refresh.png"));
        refeshButton.setSize(16);
        refeshButton.setShowFocused(false);
        refeshButton.setShowRollOver(false);
        refeshButton.setShowDown(false);
        refeshButton.setTooltip("Refresh attendance report");
        refeshButton.addClickHandler(new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                SC.say("Refresh attendance report");
            }
        });

        reportSection.setControls(refeshButton);
        reportSection.addItem(reportViewer);

        SelectItem scheduleDay = new SelectItem();
        scheduleDay.setTitle(ECApplication.getConstants().date());
        scheduleDay.setWidth(250);
        scheduleDay.setWrapTitle(false);
        scheduleDay.setOptionDataSource(ScheduleDS.getInstance());
        scheduleDay.setValueField(Parameter.ID);
        scheduleDay.setDisplayField(Parameter.DATE_OF_YEAR);
        scheduleDay.addChangeHandler(new ChangeHandler() {

            @Override
            public void onChange(ChangeEvent event) {
            }
        });

        DynamicForm form = new DynamicForm();
        form.setHeight(1);
        form.setWidth(75);
        form.setNumCols(1);
        form.setFields(scheduleDay);

        ListGridField lastNameField = new ListGridField(Parameter.LAST_NAME, ECApplication.getConstants().lastName());
        ListGridField firstNameField = new ListGridField(Parameter.FIRST_NAME, ECApplication.getConstants().firstName());
        ListGridField statusField = new ListGridField(Parameter.STATUS, ECApplication.getConstants().status());
        statusField.setOptionDataSource(AttendanceStatusDS.getInstance());
        ListGridField commentField = new ListGridField(Parameter.COMMENT, ECApplication.getConstants().comment());

        attendanceList = new AttendanceGrid(AttendanceDS.getInstance());
        attendanceList.setFields(lastNameField, firstNameField, statusField, commentField);

        SectionStackSection attendanceSection = new SectionStackSection(ECApplication.getConstants().attendance());
        attendanceSection.setControls(form);
        attendanceSection.addItem(attendanceList);
        attendanceSection.setExpanded(true);

        stack.setSections(reportSection, attendanceSection);

        mainLayout.addMember(stack);
        return mainLayout;
    }

    @Override
    protected String getHelpContent() {
        return "This is help content for attendance function";
    }

    /* (non-Javadoc)
     * @see com.binhthuan.catechist.client.AbstractContent#getPrintPreviewPanel()
     */
    @Override
    protected Canvas getPrintPreviewPanel() {
        return reportViewer;
    }
}
